package com.newegg.app.qec.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.newegg.app.qec.R;
import com.newegg.app.qec.util.APIMethod;
import com.newegg.app.qec.util.BaseActivity;
import com.newegg.app.qec.util.Constants;
import com.newegg.app.qec.util.JudgeUtil;
import com.newegg.app.qec.util.MyApplication;
import com.newegg.app.qec.util.ToastUtil;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Allen on 2017/12/7.
 */
public class SetPassWordActicity extends BaseActivity implements View.OnClickListener {

    private ImageView back;
    private TextView name;
    private EditText password_et1, password_et2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setpassword);
        back = (ImageView) findViewById(R.id.img_back);
        name = (TextView) findViewById(R.id.title_name);
        password_et1 = (EditText) findViewById(R.id.password_et1);
        password_et2 = (EditText) findViewById(R.id.password_et2);
        Button sure_btn = (Button) findViewById(R.id.sure_btn);
        name.setText("设置密码");
        back.setOnClickListener(this);
        sure_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.sure_btn:
                String password1 = password_et1.getText().toString();
                String password2 = password_et2.getText().toString();
                if (!TextUtils.isEmpty(password1) && !TextUtils.isEmpty(password2)) {
                    if (password1.equals(password2)) {
                        if (JudgeUtil.isPassword(password1)) {
                            submitPassword(password1);
                        } else {
                            ToastUtil.showToast(SetPassWordActicity.this, "请输入6-16位字符，包括数字和字母");
                        }

                    } else {
                        ToastUtil.showToast(SetPassWordActicity.this, "两次输入密码不一致");
                    }
                } else {
                    ToastUtil.showToast(SetPassWordActicity.this, "请输入密码");
                }
                break;
        }
    }

    private void submitPassword(final String password) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.i("sure", Constants.getPhoneNumberByName(SetPassWordActicity.this, "phonenumber") + "@" + password + "@" + Constants.getRegisterTokenByName(SetPassWordActicity.this, "registertoken") + "@" + Constants.getRegisterCodeByName(SetPassWordActicity.this, "registercode"));
                    RequestBody formBody = new FormBody.Builder()
                            .add("tel", Constants.getPhoneNumberByName(SetPassWordActicity.this, "phonenumber"))
                            .add("emailAddress", Constants.getPhoneNumberByName(SetPassWordActicity.this, "phonenumber") + "@qq.com")
                            .add("password", password)
                            .add("securityCodeToken", Constants.getRegisterTokenByName(SetPassWordActicity.this, "registertoken"))
                            .add("loginProvider", Constants.getRegisterCodeByName(SetPassWordActicity.this, "registercode"))
                            .add("providerKey", Constants.getRegisterCodeByName(SetPassWordActicity.this, "registercode")).build();
                    Request request = new Request.Builder().url(APIMethod.Register).post(formBody).build();
                    Response response = MyApplication.okHttpClient.newCall(request).execute();
                    int code = response.code();
                    String bodys = response.body().string();
                    Log.i("sumbit", code + "");
                    if (code == 204 || code == 200) {
                        Message msg2 = new Message();
                        msg2.what = 0;
                        msg2.obj = bodys;
                        handler.sendMessage(msg2);
                    } else {
                        Message msg1 = new Message();
                        msg1.what = 1;
                        msg1.obj = bodys;
                        handler.sendMessage(msg1);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    //验证验证码的handler
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    Intent intent = new Intent();
                    intent.setClass(SetPassWordActicity.this, LoginActivity.class);
                    startActivity(intent);
                    SetPassWordActicity.this.finish();
                    ToastUtil.showToast(SetPassWordActicity.this, "注册成功");
                    break;
                case 1:
                    ToastUtil.showToast(SetPassWordActicity.this, "注册失败");
                    break;
            }
        }
    };
}
